/*
 * @Author: hexudong 1264275442@qq.com
 * @Date: 2023-04-11 14:05:40
 * @LastEditors: hexudong 1264275442@qq.com
 * @LastEditTime: 2023-04-23 15:51:54
 * @FilePath: /worker_supervision_oa/src/components/BorrowDetail/index.tsx
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import React, { useState, useEffect } from 'react';
import { Descriptions, Modal } from 'antd';
import { GridContent } from '@ant-design/pro-layout';
import { PageContainer } from '@ant-design/pro-components';
import Base from './base';
import { accDiv } from '@/utils/utils';
import { borrowDetail } from '@/services/public';
import FileOpen from '@/components/FileIframOpen';

interface BorrowDetailProps {
  query: any;
  type: 'construction' | 'bank' | 'labor' | 'guarantee' | 'platform'; //施工端,银行端,牢监端,担保端,平台端
  isModal?: boolean;
  visible?: boolean;
  status: number;
  onSubmit?: (load?: boolean) => void;
}
const BorrowDetail: React.FC<BorrowDetailProps> = ({
  query,
  type,
  isModal,
  visible,
  status,
  onSubmit,
}) => {
  const [detailMessage, setDetailMessage] = useState<any>({});
  const [showBase, setShowBase] = useState<boolean>(false);
  const [showFile, setShowFile] = useState<boolean>(false);
  const [fileUrl, setFileUrl] = useState<string>('');

  const getDetail = async () => {
    console.log(type, 'type');

    const res = await borrowDetail({
      type,
      id: query.id,
    });
    switch (res.data.loanStatus) {
      case 0:
        res.data.loanStatusText = '待审核';
        break;
      case 1:
        res.data.loanStatusText = '平台审核通过';
        break;
      case 2:
        res.data.loanStatusText = '担保公司审核通过';
        break;
      case 3:
        res.data.loanStatusText = '银行审核通过';
        break;
      case 4:
        res.data.loanStatusText = '劳监备案通过';
        break;
      case 5:
        res.data.loanStatusText = '审核拒绝';
        break;
      case 6:
        res.data.loanStatusText = '发放贷款成功';
        break;
      default:
        res.data.loanStatusText = '发放贷款失败';
        break;
    }
    if (res && res.success) {
      setDetailMessage(res.data);
      setShowBase(true);
    }
  };

  useEffect(() => {
    console.log(status, 'key');

    getDetail();
  }, []);

  const content = (
    <div>
      <Descriptions
        size="small"
        title={detailMessage.projectName || ''}
        column={3}
        labelStyle={{ fontWeight: 'bold' }}
      >
        <Descriptions.Item label="所属劳监局">{detailMessage.departmentName}</Descriptions.Item>
        <Descriptions.Item label="施工单位">{detailMessage.corpName}</Descriptions.Item>
        <Descriptions.Item label="中标价或发包金额">
          {detailMessage.guaranteeAmount
            ? accDiv(detailMessage.guaranteeAmount || 0, 1000000) + '（万元）'
            : '0万元'}
        </Descriptions.Item>
      </Descriptions>
      <Descriptions size="small" column={3} labelStyle={{ fontWeight: 'bold' }}>
        <Descriptions.Item label="工资专户开户银行">{detailMessage.bankName}</Descriptions.Item>
        <Descriptions.Item label="账户账号">{detailMessage.applyAccount}</Descriptions.Item>
        <Descriptions.Item label="余额">
          {detailMessage.accountBalance ? accDiv(detailMessage.accountBalance || 0, 100) : '0'}(元)
        </Descriptions.Item>
        <Descriptions.Item label="已发放贷款金额">
          {detailMessage.loanBalance ? accDiv(detailMessage.loanBalance || 0, 100) : '-'}(元)
        </Descriptions.Item>
        <Descriptions.Item label="施工合同/分包合同">
          <a
            onClick={() => {
              setFileUrl(detailMessage.constructionContractUrl);
              setShowFile(true);
            }}
          >
            查看
          </a>
        </Descriptions.Item>
        <Descriptions.Item label="工资发放证明">
          <a
            onClick={() => {
              setFileUrl(detailMessage.sixSalaryFileUrl);
              setShowFile(true);
            }}
          >
            查看
          </a>
        </Descriptions.Item>

      </Descriptions>
    </div>
  );
  return (
    <>
      {isModal ? (
        <Modal
          title="贷款详情"
          visible={visible}
          width={1000}
          onCancel={() => onSubmit && onSubmit(false)}
          maskClosable={false}
          footer={null}
        >
          <GridContent>
            {content}
            {showBase && (
              <Base
                detailMessage={detailMessage}
                status={status}
                type={type}
                query={query}
                onSubmit={() => {
                  if (onSubmit) {
                    onSubmit(true);
                  }
                }}
              />
            )}
          </GridContent>
        </Modal>
      ) : (
        <PageContainer
          header={{
            title: '贷款详情',
            breadcrumb: {},
            onBack: () => {
              history.back();
            },
          }}
          content={content}
        >
          <GridContent>
            {showBase && (
              <Base
                detailMessage={detailMessage}
                type={type}
                status={status}
                query={query}
                onSubmit={() => {
                  history.back();
                }}
              />
            )}
          </GridContent>
        </PageContainer>
      )}
      {showFile && (
        <FileOpen
          fielUrl={fileUrl}
          modalVisible={showFile}
          onCancel={() => {
            setShowFile(false);
            setFileUrl('');
          }}
        />
      )}
    </>
  );
};
export default BorrowDetail;
